import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import userlist from '../components/user/userlist.vue'
import productlist from '../components/product/productlist.vue'
import orderlist from '../components/orders/orderlist.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home', component: Home, 
    redirect:'/Welcome',
    children: [
      { path: '/Welcome', component: Welcome },
      { path: '/productlist', component: productlist },
      { path: '/orderlist', component: orderlist },
      { path: '/users', component: userlist }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  const tokenstr = window.sessionStorage.getItem('token');
  if (!tokenstr) return next('/login');
  next();
})
export default router
